package com.df.gameserver.scriptmanager;

import com.df.script.AbstractScriptManager;
import com.df.script.IScript;
import com.df.utils.ClassLoadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class MsgScriptManager extends AbstractScriptManager {
    private enum Single {
        /**
         * 单例对象的实例
         */
        INSTANCE;
        private MsgScriptManager instance;

        Single() {
            this.instance = new MsgScriptManager();
        }
    }

    public static MsgScriptManager getInstance() {
        return Single.INSTANCE.instance;
    }

    private MsgScriptManager(){}


    /**
     * 加载所有消息处理脚本
     */
    @Override
    public void loadScripts() {
//        scripts.put(1,new C2S_SyncPositionScript());
        List<Class<IScript>> classes = ClassLoadUtil.loadClass("script.msg", IScript.class);
        if (classes != null && classes.size() > 0) {
            for (Class<IScript> aClass : classes) {
                try {
                    IScript iScript = aClass.newInstance();
                    if (iScript.getScriptId() == null) {
                        log.error("消息处理脚本未正确返回脚本id：" + aClass.getName());
                        System.exit(-1);
                    }
                    if (scripts.contains(iScript.getScriptId())) {
                        IScript old = scripts.get(iScript.getScriptId());
                        log.error("消息处理脚本 " + aClass.getName() + "与 " + old.getClass().getName() + "  脚本id重复，脚本id：" + old.getScriptId());
                        System.exit(-1);
                    }
                    scripts.put(iScript.getScriptId(), iScript);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }

        }
    }


}
